package com.hhh.springai_test.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.springai_test.common.BaseResponse;
import com.hhh.springai_test.common.ResultUtils;
import com.hhh.springai_test.constant.UserConstant;
import com.hhh.springai_test.model.dto.postLike.AddPostLikeDTO;
import com.hhh.springai_test.model.po.CommunityPost;
import com.hhh.springai_test.model.po.User;
import com.hhh.springai_test.model.vo.CommunityPostVO;
import com.hhh.springai_test.model.vo.UserVo;
import com.hhh.springai_test.service.CommunityPostLikeService;
import com.hhh.springai_test.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/post/like")
public class CommunityPostLikeController {

    @Resource
    private CommunityPostLikeService communityPostLikeService;

    @Resource
    private UserService userService;

    /**
     * 点赞/取消点赞帖子
     * @return true-已点赞 false-未点赞
     */
    @PostMapping("/action")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public BaseResponse<Boolean> likeAction(@RequestBody @Valid AddPostLikeDTO addPostLikeDTO) {
        Long userId = StpUtil.getLoginIdAsLong();
        Boolean result = communityPostLikeService.likeOrUnlike(addPostLikeDTO.getPostId(), userId);
        return ResultUtils.success(result);
    }

    /**
     * 获取点赞状态
     */
    @GetMapping("/status/{postId}")
    public BaseResponse<Boolean> getLikeStatus(@PathVariable Long postId) {
        Long userId = StpUtil.getLoginIdAsLong();
        Boolean result = communityPostLikeService.isLiked(postId, userId);
        return ResultUtils.success(result);
    }

    /**
     * 获取用户点赞的帖子列表
     * @param current 当前页码
     * @param pageSize 每页大小
     * @return 帖子列表分页数据
     */
    @GetMapping("/my/list")
    @SaCheckRole(UserConstant.DEFAULT_ROLE)
    public BaseResponse<Page<CommunityPostVO>> getUserLikedPosts(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long pageSize) {
        Long userId = StpUtil.getLoginIdAsLong();
        Page<CommunityPost> postPage = communityPostLikeService.getUserLikedPosts(userId, current, pageSize);
        List<CommunityPost> records = postPage.getRecords();
        ArrayList<CommunityPostVO> postVOS = new ArrayList<>();
        for (CommunityPost record : records) {
            Long createuser = record.getCreateuser();
            User user = userService.getById(createuser);
            UserVo userVo = BeanUtil.copyProperties(user, UserVo.class);
            CommunityPostVO postVO = BeanUtil.copyProperties(record, CommunityPostVO.class);
            postVO.setUserVo(userVo);
            postVOS.add(postVO);
        }
        Page<CommunityPostVO> communityPostVOPage = new Page<>(postPage.getCurrent(), postPage.getSize(), postPage.getTotal());
        communityPostVOPage.setRecords(postVOS);
        return ResultUtils.success(communityPostVOPage);
    }
} 